package com.ecy.quadratic.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.ComicUpdateAdapter;
import com.ecy.quadratic.beans.ComicRecommend;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.component.DaggerComicMainComponent;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.activity.ComicDetailActivity;
import com.ecy.quadratic.ui.contract.ComicUpdateContract;
import com.ecy.quadratic.ui.presenter.ComicUpdatePresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class ComicUpdateFragment extends BaseFragment implements ComicUpdateContract.View {
    private static final String UPDATE_INDEX = "update_index";
    @Bind(R.id.comic_update_swipe_refreshview)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.comic_update_listview)
    ListView mNovelListView;
    @Inject
    ComicUpdatePresenter mUpdatePresenter;
    private ComicUpdateAdapter mAdapter;
    private List<ComicRecommend> mNovelList;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;
    private int mUpdateIndex = 0;
    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            mUpdatePresenter.getRecommendList("1");
        }
    };

    public static ComicUpdateFragment newInstance(int index) {
        ComicUpdateFragment fragment = new ComicUpdateFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(UPDATE_INDEX, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_comic_update;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerComicMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mUpdatePresenter != null) {
            mUpdatePresenter.attachView(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUpdatePresenter != null)
            mUpdatePresenter.detachView();
    }

    @Override
    public void initData() {
        mUpdateIndex = getArguments().getInt(UPDATE_INDEX);
        mNovelList = new ArrayList<ComicRecommend>();
        mAdapter = new ComicUpdateAdapter(getContext(), mNovelList);
    }

    @Override
    public void configViews() {
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                mSwipeRefreshView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
        mNovelListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ComicDetailActivity.startActivity(getContext(), mNovelList.get(position));
            }
        });
        mNovelListView.setAdapter(mAdapter);
    }

    @Override
    public void showRecommendList(PageInfo<ComicRecommend> pageInfo) {
        mCurrentPage = pageInfo.pageNum;
        mTotalPage = pageInfo.pages;
        mNovelList.addAll(pageInfo.list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
    }
}
